import request from '@/utils/request'
import type { ApiResponse } from '@/types/api'

export interface GeneratedImage {
  id: number
  userId: number
  seed: number
  truncationPsi: number
  imageUrl: string
  ossPath: string
  createTime: string
  updateTime: string
}

export interface ImageGenerationParams {
  seed: number
  truncationPsi: number
}

export interface ImageHistoryParams {
  limit?: number
  page?: number
  userId?: number
}

// 生成图片
export const predictImage = (params: ImageGenerationParams): Promise<ApiResponse<GeneratedImage>> => {
  return request.post('/image/predictImage', params)
}

// 获取历史记录
export const getHistory = (params: ImageHistoryParams = {}): Promise<ApiResponse<GeneratedImage[]>> => {
  return request.get('/image/history', { params })
}

// 获取用户头像
export const getUserImage = (userId: number): Promise<ApiResponse<GeneratedImage>> => {
  return request.get('/image/userImage', { params: { userId } })
}

// 上传头像
export const uploadAvatar = (file: File): Promise<ApiResponse<GeneratedImage>> => {
  const formData = new FormData()
  formData.append('file', file)
  return request.post('/image/uploadAvatar', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 删除生成的图片
export const deleteImage = (imageId: number): Promise<ApiResponse<void>> => {
  return request.delete(`/image/${imageId}`)
}

// 获取图片详情
export const getImageDetail = (imageId: number): Promise<ApiResponse<GeneratedImage>> => {
  return request.get(`/image/${imageId}`)
}
